Hi everyone,

my question is simple : why the code below does not work ? I get a segfault when trying to affect mat[i][j].

Code:
#include <stdio.h>

void init (int ** mat, int numberLines, int numberCol) {
  int i = 0,
      j = 0;
  for (i = 0; i < numberLines; i++){
    for (j = 0; j < numberCol; j++) {
      mat[i][j] = 0;
    }
  }
}

int main () {
  int mat[3][4];
  init ((int **)mat, 3, 4);
  return 0;
}
If I make the initialization in the main, it works perfectly well. Declaring and mallocatin an int ** instead of an int [3][4] also works, as in the code below :
Code:
#include <stdio.h>
#include <stdlib.h>

void init (int ** mat, int numberLines, int numberCol) {
  int i = 0,
      j = 0;
  for (i = 0; i < numberLines; i++){
    for (j = 0; j < numberCol; j++) {
      mat[i][j] = 0;
    }
  }
}

int main () {
  int ** mat;
  int i = 0;

  mat = malloc(sizeof(int *) * 3);
  for (i = 0; i < 3; i++){
    *(mat + i) = malloc(sizeof(int) * 4);
  }

  init (mat, 3, 4);
  return 0;
I really don't get it !

Thanks !
Nicolas